﻿using AutoMapper;
using MediatR;
using PersonnelManagement.Application.Command.ClientCommand;
using PersonnelManagement.Infrstructre.Interface;

namespace PersonnelManagement.Application.CommandHandler.ClientCommandHandlers
{
    public class ClientStateCommandHandlers : IRequestHandler<ClientStateCommand, int>
    {
        IClientRepository cRep;
        IMapper mapper;
        public ClientStateCommandHandlers(IClientRepository cRep, IMapper mapper)
        {
            this.cRep = cRep;
            this.mapper = mapper;
        }
        public async Task<int> Handle(ClientStateCommand request, CancellationToken cancellationToken)
        {
            var list = (await cRep.Getall()).Where(x =>x.ClientId==request.ClientId).First();
            //点申请 传2 
             if(request.CPickingStatus==2)
            {
               list.CPickingStatus=request.CPickingStatus;
               list.CApplicant = request.CApplicant;
            }
             //点通过 传3
             if(request.CPickingStatus == 3)
            {
                list.CPickingStatus = request.CPickingStatus;
                list.CRecipients=request.CRecipients;
                list.CPickTime=DateTime.Now;
            }
            //点不通过 传1
            if (request.CPickingStatus == 1)
            {
                list.CPickingStatus =1;
                list.CRecipients = "";
                list.CApplicant = "";
                list.CPickTime = null;
            }
            //点指派 传4
            if(request.CPickingStatus == 4)
            {
                list.CPickingStatus = 3;
                list.CRecipients=request.CRecipient;
                list.CPickTime = DateTime.Now;
            }
            //点公用 传5
            if (request.CPickingStatus == 5)
            {
                list.CPickingStatus = 3;
                list.CRecipients = "";
                list.CPickTime = DateTime.Now;
            }
            await cRep.UpdateAsymc(list);
            return 1;
        }
    }
}
